package com.sjy.framework.config;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import com.sjy.common.constant.Constants;
import com.sjy.framework.interceptor.RepeatSubmitInterceptor;
import com.sjy.framework.interceptor.TokenInterceptor;
import com.sjy.common.utils.TokenUtil;

/**
 * 通用配置
 * 
 * @author sjy
 */
@Configuration
public class ResourcesConfig implements WebMvcConfigurer
{
    /**
     * 首页地址
     */
    @Value("${shiro.user.indexUrl}")
    private String indexUrl;

    @Autowired
    private RepeatSubmitInterceptor repeatSubmitInterceptor;
	@Autowired
	private TokenUtil tokenUtil;

    /**
     * 默认首页的设置，当输入域名是可以自动跳转到默认指定的网页
     */
    @Override
    public void addViewControllers(ViewControllerRegistry registry)
    {
        registry.addViewController("/").setViewName("forward:" + indexUrl);
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry)
    {
        /** 本地文件上传路径 */
        registry.addResourceHandler(Constants.RESOURCE_PREFIX + "/**").addResourceLocations("file:" + SjyConfig.getProfile() + "/");

        /** swagger配置 */
        registry.addResourceHandler("/swagger-ui/**").addResourceLocations("classpath:/META-INF/resources/webjars/springfox-swagger-ui/");
    }

    /**
     * 自定义拦截规则
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry)
    {
    	List<String> patterns = new ArrayList<String>();
    	//TODO:这里添加要证书验证的路径
    	//patterns.add("/login");
    	//patterns.add("/index");
    	//patterns.add("/activiti/task/*");
    	//patterns.add("/public/weixin/*");
    	patterns.add("/api/**");
        List<String> excludePath = new ArrayList<>();
        
        registry.addInterceptor(repeatSubmitInterceptor).addPathPatterns("/**");
        registry.addInterceptor(new TokenInterceptor(tokenUtil)).addPathPatterns(patterns).excludePathPatterns(excludePath);
        
    }
}